package org.example.clouddemo.product.service.common.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.clouddemo.bean.common.entity.Product;
import org.example.clouddemo.product.service.common.mapper.ProductMapper;
import org.example.clouddemo.product.service.common.ProductService;
import org.springframework.stereotype.Service;

import java.util.List;


/**
* 产品表 服务实现类
*/
@RequiredArgsConstructor
@Service // 业务逻辑层
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    private final ProductMapper productMapper;

    /**
     * 基于查询条件(Product)对产品（Product）进行分页查询
     * @param product 传入产品对象作为查询条件
     * @return 返回产品对象
     */
    @Override
    public IPage<Product> selectPageVo(Page<Product> page, Product product) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>(product);
        queryWrapper.orderByDesc("create_time"); // 按照创建时间进行倒序排列
        return productMapper.selectPage(page,queryWrapper);
    }

    /**
     * 查询产品
     * @param pid 传入产品id
     * @return 返回产品对象
     */
    @Override
    public Product selectOne(Long pid) {
        return productMapper.selectById(pid);
    }

    /**
     * 创建产品
     * @param product 传入产品对象
     * @return 返回改变行数
     */
    @Override
    public int create(Product product) {
        return productMapper.insert(product);
    }

    /**
     * 更新产品
     * @param product 传入产品对象
     * @return 返回改变行数
     */
    @Override
    public int update(Product product) {
        return productMapper.updateById(product);
    }

    @Override
    public int delete(String id) {
        return productMapper.deleteById(id);
    }

    @Override
    public List<Product> selectOrderByPriceDesc() {
        return  productMapper.selectOrderByPriceDesc();
    }

}
